﻿using System.Windows.Forms;
using KillBill.Business.Entities;
using KillBill.Presentation.Common.WindowsForms.View;
using KillBill.Presentation.Modules.Suppliers.IView;
using KillBill.Presentation.Modules.Suppliers.Presenter;


namespace KillBill.Presentation.Modules.Suppliers.View
{
    public partial class ProviderAddEditView : KillBillWinFormView, IProviderAddEdit
    {
        public ProviderAddEditPresenter Presenter { get; set; }
        public bool IsEditing { get; set; }

        private Provider _provider;

        public ProviderAddEditView()
        {
            InitializeComponent();
            Presenter = new ProviderAddEditPresenter(this);
            IsEditing = false;
        }

        public ProviderAddEditView(Provider provider)
        {
            InitializeComponent();
            Presenter = new ProviderAddEditPresenter(this, provider);
            IsEditing = true;
        }

        public Provider Provider
        {
            get {
                _provider.Person = UscPerson.Person;
                _provider.ProviderRUC = TxtRUC.Text;
                _provider.ContactID = UscPerson.Person.PersonID;
                _provider.Name = TxtName.Text;
                _provider.IsActive = ChkIsActive.Checked;
                return _provider;
            }
            set
            {
                UscPerson.Person = value.Person;
                TxtRUC.Text = value.ProviderRUC;
                TxtName.Text = value.Name;
                ChkIsActive.Checked = value.IsActive;
                _provider = value;
            }
        }

        private void BtnSave_Click(object sender, System.EventArgs e)
        {

            if (UscPerson.IsValid && TxtRUC.IsValid && TxtName.IsValid)
                Presenter.Save();
            else
                this.ShowMessage("Existen datos incorrectos");
        }

        private void BtnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
    }
}
